<?php
namespace Home\Controller;

use Utuo\DistributionShopForeground;
use My\Model\DistributionShopPackageModel;
use My\Model\OrderModel;

class DisShopAgentShopController extends DistributionShopForeground {
    
    /**
     * 初始化
     */
    public function _init() {
        layout('disshop');
        $this->setModelName('DistributionShop');
        $this->setModelControllerName('DisShop');
        if ( !in_array(ACTION_NAME, array()) ) { // 排除不支持分享跳转的链接
            $this->setSupportRedirect(isset($_REQUEST['debug'])?false:true);
        }
    }
    
    /**
     * 代理套餐
     */
    public function indexAction() {
        $packageList =  M("DistributionShopPackage")->where("cid=%d and status=1 and is_deleted=0", $this->cid)->select();
        foreach ( $packageList as &$v ) {
            $v['url'] = U("DisShopAgentShop/show", array("sid"=>$this->cid, "id" => $v['id']));
        }
        $this->assign("packageList", $packageList);
        $this->display("{$this->modelControllerName}/agentShopIndex");
    }
    
    /**
     * 查看商品
     */
    public function showAction() {
        $id = I("id", 0, "intval");
        $shopPackage =  M("DistributionShopPackage")->where("cid=%d and id=%d", $this->cid, $id)->find();
        if ( $shopPackage ) {
            $goodsList = M("DistributionShopPackageGoods a")->join("__DISTRIBUTION_SHOP_GOODS__ b on a.goodsId=b.id", "LEFT")->where("packageId=%d", $shopPackage['id'])->field("b.*,a.count")->select();
            foreach ( $goodsList as $k => &$v ) {
                $v["images"] = unserialize($v["images"]);
            }
            $shopPackage['goodsList'] = $goodsList;
        }
        $shopPackage or $this->ajaxError("商品已下架");
        $this->assign("shopPackage", $shopPackage);
        $this->display("{$this->modelControllerName}/agentShopShow");
    }
    
}